<?php
$mySQLMarca = "select ma.nombre from marcas ma join modelos mo on ma.idmarca = mo.idmarca join ordenes o on mo.idmodelo = o.idmodelo where idorden =".$idOrden;

$sql = "select codigo from versionesdesoftwarexoperador where idmodelo = $idModelo and idoperador = $idOperador";
$result = readSQL($sql);
$versionDeSoftwareDeSalida = '';
foreach($result as $value)
{
	$versionDeSoftwareDeSalida = $value['codigo'];
	break;
}

$resultMarca = readSQL($mySQLMarca);
foreach($resultMarca as $valueMarca)
{
	$marca = strtoupper($valueMarca['nombre']);
}

if($_GET['swap']==1 || $fix)
{
	$mantieneGarantia = 1;
	if($marca == "SONY ERICSSON")
	{
		$nombreMarca = "sony";
	}
	else
	{
		$nombreMarca = strtolower($marca);	
	}
	
	if($fix)
	{
		$IMEI = $imei;
	}
	
	$sql = "select * from ordenes$nombreMarca where idorden in (select idorden from ordenes where imei = $IMEI";
	
	if($_GET['swap'])
	{
		$sql .= " and length(imeisalida) =15";
	}
	
	$sql .= " order by idorden desc) order by idorden desc limit 1";

	//echo $sql;
	//exit;
	$result = readSQL($sql);
}

if($mantieneGarantia)
{
	switch($marca)
	{
		case "NOKIA":
		{
			if($_GET['swap']==1)
			{
				foreach($result as $value)
				{
					$casoDeReparacion = $value['casoDeReparacion'];
					$codigoProducto = $value['codigoProducto'];
					$tipoCliente = $value['tipoCliente'];
					$tipoRetorno = $value['tipoRetorno'];
					$VID = $value['VID'];
					$PSN = $value['PSN'];
					$inputSwVer = $value['inputSwVer'];
					$outputSwVer = /*$versionDeSoftwareDeSalida;//*/$value['outputSwVer'];
					$yearCode = $value['yearCode'];
					$monthCode = $value['monthCode'];
				}
			}
			else
			{
				$casoDeReparacion = $_POST['txtNokiaCasoDeReparacion'];
				$codigoProducto = $_POST[txtNokiaCodigoProducto];
				$tipoCliente = $_POST[txtNokiaTipoCliente];
				$tipoRetorno = $_POST[txtNokiaTipoRetorno];
				$VID =  $_POST[txtNokiaVID];
				$PSN = $_POST[txtNokiaPSN];
				$inputSwVer = $_POST[txtNokiaInputSW];
				$outputSwVer = /*$versionDeSoftwareDeSalida;//*/$_POST[txtNokiaOutputSW];
				$yearCode = $_POST['txtNokiaYearCode'];
				$monthCode = $_POST['txtNokiaMonthCode'];
			}
			
			if($reinsercion || $fix)
			{
				if(trim($casoDeReparacion)=='')
				{
					$mySQL = "update ordenesnokia set codigoProducto = '$codigoProducto', tipoCliente = '$tipoCliente', tipoRetorno ='$tipoRetorno', VID = '$VID', PSN = '$PSN', inputSwVer = '$inputSwVer', outputSwVer = '$outputSwVer', yearCode = '$yearCode', monthCode = '$monthCode' where idOrden = ".$idOrden;
				}
				else
				{
					$mySQL = "update ordenesnokia set casoDeReparacion = '$casoDeReparacion' where idOrden = ".$idOrden;
				}
			}
			else
			{
				if(trim($casoDeReparacion)=='')
				{
					$mySQL = "insert into ordenesnokia set idOrden = $idOrden, codigoProducto = '$codigoProducto', tipoCliente = '$tipoCliente', tipoRetorno ='$tipoRetorno', VID = '$VID', PSN = '$PSN', inputSwVer = '$inputSwVer', outputSwVer = '$outputSwVer', yearCode = '$yearCode', monthCode = '$monthCode'";
				}
				else
				{
					$mySQL = "insert into ordenesnokia set idOrden = $idOrden, casoDeReparacion = '$casoDeReparacion'";
				}
			}
			//echo $mySQL;
			//exit;
			executeSQL($mySQL);
			break;
		}
		case "LG":
		{
			if($_GET['swap']==1)
			{
				foreach($result as $value)
				{
					$serialNumber = $value['serialNumber'];
					$tipoServicio = $value['tipoServicio'];
					$inputSwVer = $value['inputSwVer'];
					$outputSwVer = $value['outputSwVer'];
					$lgTipoCliente = $value['tipoCliente'];
				}
			}
			else
			{
				$serialNumber = $_POST[txtLgSerialNumber];
				$tipoServicio = $_POST[txtLgTipoServicio];
				$inputSwVer = $_POST[txtLgInputSW];
				$outputSwVer =  $_POST[txtLgOutputSW];
				$lgTipoCliente = $_POST[lgTipoCliente];
			}
			
			if($reinsercion || $fix)
			{
				$mySQL = "update ordeneslg set serialNumber = '$serialNumber', tipoServicio = '$tipoServicio', inputSwVer ='$inputSwVer', outputSwVer = '$outputSwVer', tipoCliente = '$lgTipoCliente' where idOrden = ".$idOrden;
			}
			else
			{
				$mySQL = "insert into ordeneslg set idOrden = $idOrden, serialNumber = '$serialNumber', tipoServicio = '$tipoServicio', inputSwVer ='$inputSwVer', outputSwVer = '$outputSwVer', tipoCliente = '$lgTipoCliente'";
				/*
				$mySQL = "INSERT INTO ordeneslg(idOrden, serialNumber, tipoServicio, inputSwVer, outputSwVer) VALUES(";
				$mySQL = $mySQL . $idOrden . ", ";
				$mySQL = $mySQL . "'" . $_POST[txtLgSerialNumber] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtLgTipoServicio] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtLgInputSW] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtLgOutputSW] . "')";
				*/
			}
			executeSQL($mySQL);
			break;
		}
		case "ALCATEL":
		{
			if($_GET['swap']==1)
			{
				foreach($result as $value)
				{
					$referenciaComercial = $value['referenciaComercial'];
					$statusIn = $value['statusIn'];
					$inputSwVer = $value['inputSwVer'];
					$outputSwVer = /*$versionDeSoftwareDeSalida;//*/$value['outputSwVer'];
				}
			}
			else
			{
				$referenciaComercial = $_POST[txtAlcatelReferenciaComercial];
				$statusIn = $_POST[txtAlcatelStatusIN];
				$inputSwVer = $_POST[txtAlcatelInputSW];
				$outputSwVer = /*$versionDeSoftwareDeSalida;//*/$_POST[txtAlcatelOutputSW];
			}
			
			if($reinsercion || $fix)
			{
				$mySQL = "update ordenesalcatel set referenciaComercial = '$referenciaComercial', statusIn = '$statusIn', inputSwVer ='$inputSwVer', outputSwVer = '$outputSwVer' where idOrden = ".$idOrden;
			}
			else
			{
				$mySQL = "insert into ordenesalcatel set idOrden = $idOrden, referenciaComercial = '$referenciaComercial', statusIn = '$statusIn', inputSwVer ='$inputSwVer', outputSwVer = '$outputSwVer'";
				
				/*
				$mySQL = "INSERT INTO ordenesalcatel(idOrden, referenciaComercial, statusIn, inputSwVer, outputSwVer) VALUES(";
				$mySQL = $mySQL . $idOrden . ", ";
				$mySQL = $mySQL . "'" . $_POST[txtAlcatelReferenciaComercial] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtAlcatelStatusIN] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtAlcatelInputSW] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtAlcatelOutputSW] . "')";
				*/
			}
			executeSQL($mySQL);
			break;
		}
		case "SAMSUNG":
		{
			if($_GET['swap']==1)
			{
				foreach($result as $value)
				{
					$serialNumber = $value['serialNumber'];
					$tipoReparacion = $value['tipoReparacion'];
				}
			}
			else
			{
				$serialNumber = $_POST[txtSamsungSerialNumber];
				$tipoReparacion = $_POST[txtSamsungTipoReparacion];
			}
			
			if($reinsercion || $fix)
			{
				$mySQL = "update ordenessamsung set serialNumber = '$serialNumber', tipoReparacion = '$tipoReparacion' where idOrden = ".$idOrden;
			}
			else
			{
				$mySQL = "insert into ordenessamsung set idOrden = $idOrden, serialNumber = '$serialNumber', tipoReparacion = '$tipoReparacion'";
				/*
				$mySQL = "INSERT INTO ordenessamsung(idOrden, serialNumber, tipoReparacion) VALUES(";
				$mySQL = $mySQL . $idOrden . ", ";
				$mySQL = $mySQL . "'" . $_POST[txtSamsungSerialNumber] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtSamsungTipoReparacion] . "')";
				*/
			}
			executeSQL($mySQL);
			break;
		}
		case "SONY ERICSSON":
		{
			if($_GET['swap']==1)
			{
				foreach($result as $value)
				{
					$SN = $value['SN'];
					$codigoFabricacion = $value['codigoFabricacion'];
					$DPY = $value['DPY'];
					$inputSW = $value['inputSW'];
					$outputSW = /*$versionDeSoftwareDeSalida;//*/$value['outputSW'];
				}
			}
			else
			{
				$SN = $_POST[txtSonySN];
				$codigoFabricacion = $_POST[txtSonyCodigoFabricacion];
				$DPY = $_POST[txtSonyDPY];
				$inputSW = $_POST[txtSonyInputSW];
				$outputSW = /*$versionDeSoftwareDeSalida;//*/$_POST[txtSonyOutputSW];
			}
			
			if($reinsercion || $fix)
			{
				$mySQL = "update ordenessony set SN = '$SN', codigoFabricacion = '$codigoFabricacion', DPY ='$DPY', inputSW = '$inputSW', outputSW = '$outputSW' where idOrden = ".$idOrden;
			}
			else
			{
				$mySQL = "insert into ordenessony set idOrden = $idOrden, SN = '$SN', codigoFabricacion = '$codigoFabricacion', DPY ='$DPY', inputSW = '$inputSW', outputSW = '$outputSW'";
				
				/*
				$mySQL = "INSERT INTO ordenessony(idOrden, SN, codigoFabricacion, DPY, inputSW, outputSW) VALUES(";
				$mySQL = $mySQL . $idOrden . ", ";
				$mySQL = $mySQL . "'" . $_POST[txtSonySN] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtSonyCodigoFabricacion] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtSonyDPY] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtSonyInputSW] . "', ";
				$mySQL = $mySQL . "'" . $_POST[txtSonyOutputSW] . "')";
				*/
			}
			executeSQL($mySQL);
			break;
		}
		case "MOTOROLA":
		{
			if($_GET['swap']==1)
			{
				foreach($result as $value)
				{
					$MSN = $value['MSN'];
				}
			}
			else
			{
				if($_POST['cbxMotorolaMsnNoLegible']!="checked")
				{
					$mySQL = "Select descripcion from modelos where codigo = " . substr($_POST['txtModelo'],0,strpos($_POST['txtModelo'],"-") - 1);
					$result = readSQL($mySQL);
					foreach($result as $value)
					{
						$APC = $value['descripcion'];
					}
					$X = "X";
					$A = strtoupper($_POST['txtMotorolaA']);
					$M = strtoupper($_POST['txtMotorolaM']);
					$SSSS = substr($_POST['txtNroImei'],11);
					$MSN = 	$APC.$X.$A.$M.$SSSS;
				}
				else
				{
					$MSN = "";
				}
			}
			
			if($reinsercion || $fix)
			{
				$mySQL = "update ordenesmotorola set MSN = '".$MSN."' where idOrden = ".$idOrden;
			}
			else
			{
				$mySQL = "INSERT INTO ordenesmotorola set idOrden = ". $idOrden .", MSN = '". $MSN ."'";
			}
			//echo $mySQL;
			//exit;
			executeSQL($mySQL);
			break;
		}
		default:
		{
			$mySQL = "INSERT INTO ordenesfalladas set idOrden = ". $idOrden;
			executeSQL($mySQL);
			break;	
		}
	}
}
?>